import 'package:flutter/cupertino.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:package_info_plus/package_info_plus.dart';

PackageInfo? usePackageInfo() {
  return use(const _PackageInfoHook());
}

class _PackageInfoHook extends Hook<PackageInfo?> {
  const _PackageInfoHook();

  @override
  HookState<PackageInfo?, Hook<PackageInfo?>> createState() {
    return _PackageInfoHookState();
  }
}

class _PackageInfoHookState extends HookState<PackageInfo?, _PackageInfoHook> {
  PackageInfo? packageInfo;

  @override
  void initHook() {
    super.initHook();
    PackageInfo.fromPlatform().then((value) {
      packageInfo = value;
      setState(() { });
    });
  }

  @override
  PackageInfo? build(BuildContext context) {
    return packageInfo;
  }
}
